package com.osgix.common.jms;

import java.io.Serializable;

import javax.jms.Topic;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * 生产者
 **/
public class MessageProducer implements Runnable, Serializable {

	private static final long serialVersionUID = -6988594804682944656L;
	private final Log log = LogFactory.getLog(this.getClass());
	private Topic destination;
	private JmsTemplate jmsTemplate;
	private Object message;

	public MessageProducer() {

	}

	public void setMessage(Object message) {
		this.message = message;
	}

	public void sendMessage(Object message) {
		if (log.isDebugEnabled()) {
			log.debug("---------------do send Message------------------");
		}
		jmsTemplate.convertAndSend(destination, message);
		if (log.isDebugEnabled()) {
			log.debug("----------------send End !!!-----------------");
		}
	}

	public void sendMessage() {
		if (log.isDebugEnabled()) {
			log.debug("----------------do send Message-----------------");
		}
		jmsTemplate.convertAndSend(destination, message);
		if (log.isDebugEnabled()) {
			log.debug("--------------- send End !!! ------------------");
		}
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	public void setDestination(Topic destination) {
		this.destination = destination;
	}

	public void run() {
		this.sendMessage();
	}

}
